﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace SoftKeyBoard
{
    // 声明页面转向委托
    public delegate void CharKeyClickEventHandler(Object sender, CharClickEventArgs e);
    public partial class SoftAllKeyBoard : Form
    {      
        private IntPtr _parentHandle = IntPtr.Zero;
        private bool _isCapital = false;
        private System.Media.SoundPlayer player;
        public event CharKeyClickEventHandler KeyClick;
        public event EventHandler BackspaceKeyClick;

        public SoftAllKeyBoard()
        {
            InitializeComponent();
        }

        public SoftAllKeyBoard(IntPtr handle)
        {
            _parentHandle = handle;
            InitializeComponent();
        }

        public const int USER_MESSAGE_OK = 0x0405;//用户自定义消息的开始数值
        public const int USER_MESSAGE_CLEAR = 0x0406;//用户自定义消息的开始数值

        [DllImport("user32.dll")]
        public static extern void PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);

        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        private const int WS_EX_TOOLWINDOW = 0x00000080;
        private const int WS_EX_NOACTIVATE = 0x08000000;

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= (WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW);
                //cp.Parent = IntPtr.Zero; // Keep this line only if you used UserControl
                return cp;
                //return base.CreateParams;
            }
        }

        /// <summary>
        /// 启动键盘点击事件
        /// </summary>
        /// <param name="e"></param>
        protected void OnKeyClick(CharClickEventArgs e)
        {
            if (KeyClick != null)
            {
                KeyClick(this, e);
            }
        }

        /// <summary>
        /// 启动退格事件
        /// </summary>
        /// <param name="e"></param>
        protected void OnBackspaceClick(EventArgs e)
        {
            if (BackspaceKeyClick != null)
            {
                BackspaceKeyClick(this, e);
            }
        }     

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonCapsLock_Click(object sender, EventArgs e)
        {
            _isCapital = !_isCapital;
            if(_isCapital)
            {
                buttonCapsLock.FlatAppearance.BorderSize = 3;
                buttonCapsLock.FlatAppearance.BorderColor = Color.Red;
            }
            else
            {
                buttonCapsLock.FlatAppearance.BorderSize = 0;
                buttonCapsLock.FlatAppearance.BorderColor = Color.White;
            }
            buttonA.Text = _isCapital ? "A" : "a";
            buttonB.Text = _isCapital ? "B" : "b";
            buttonC.Text = _isCapital ? "C" : "c";
            buttonD.Text = _isCapital ? "D" : "d";
            buttonE.Text = _isCapital ? "E" : "e";
            buttonF.Text = _isCapital ? "F" : "f";
            buttonG.Text = _isCapital ? "G" : "g";
            buttonH.Text = _isCapital ? "H" : "h";
            buttonI.Text = _isCapital ? "I" : "i"; 
            buttonJ.Text = _isCapital ? "J" : "j";
            buttonK.Text = _isCapital ? "K" : "k";
            buttonL.Text = _isCapital ? "L" : "l";
            buttonM.Text = _isCapital ? "M" : "m";
            buttonN.Text = _isCapital ? "N" : "n";
            buttonO.Text = _isCapital ? "O" : "o";
            buttonP.Text = _isCapital ? "P" : "p";
            buttonQ.Text = _isCapital ? "Q" : "q";
            buttonR.Text = _isCapital ? "R" : "r";
            buttonS.Text = _isCapital ? "S" : "s";
            buttonT.Text = _isCapital ? "T" : "t";
            buttonU.Text = _isCapital ? "U" : "u";
            buttonV.Text = _isCapital ? "V" : "v";
            buttonW.Text = _isCapital ? "W" : "w";
            buttonX.Text = _isCapital ? "X" : "x";
            buttonY.Text = _isCapital ? "Y" : "y";
            buttonZ.Text = _isCapital ? "Z" : "z";
        }

        private void buttonBackspace_Click(object sender, EventArgs e)
        {
            OnBackspaceClick(new EventArgs());
            player.Play();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (_parentHandle != IntPtr.Zero)
            {
                this.Hide();
                int res = SendMessage(_parentHandle, USER_MESSAGE_OK, 0, 0);
                if (res == 0x00)
                {
                    this.Close();
                }
                else if (res == 0xff)
                {
                    this.Show();
                }
            }
        }

        private void SoftAllKeyBoard_Load(object sender, EventArgs e)
        {
            string path = @"Sound/click.wav";
            if (!File.Exists(path))
                return;
            player = new System.Media.SoundPlayer(path);          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string key = ((Button)sender).Text.Trim();
            OnKeyClick(new CharClickEventArgs(key));
            player.Play();
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            PostMessage(_parentHandle, USER_MESSAGE_CLEAR, 0, 0);
        }
    }
}
